package controller.main;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import Bean.memberBean;
import dao.memberDao;

public class loginController extends AbstractCommandController {
	private memberDao dao;

	public void setDao(memberDao dao) {
		this.dao = dao;
	}

	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse arg1, Object arg2, BindException arg3)
			throws Exception {

		String mid = request.getParameter("mid");
		String mpassword = request.getParameter("mpassword");

		memberBean mBean = dao.getIdInfo(mid);

		ModelAndView mav = new ModelAndView();

		int result = dao.mCheckid(mid, mpassword);
		if (result == -1) {
			mav.setViewName("loginError");
			return mav;
		} else if (result == 0) {
			mav.setViewName("loginError");
			return mav;
		} else {
			HttpSession session = request.getSession();

			session.setAttribute("member", mBean);

			mav.addObject("mBean", mBean);

			mav.setViewName("redirect:/startHome.main");
			return mav;
		}
	}
}
